package com.example.communitypro.controller;


import com.example.communitypro.mapper.UserDetailsMapper;
import com.example.communitypro.pojo.*;
import com.example.communitypro.service.*;
import com.example.communitypro.util.SpringContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
public class JumpController {

    @Autowired
    private CommunityInformationService communityInformationService;
    @Autowired
    private PostService postService;
    @Autowired
    private UserInformationService userInformationService;

    @Autowired
    private HomeMessageService homeMessageService;

    @Autowired(required=false)
    private UserDetailsMapper userDetailsMapper;

    @Autowired
    private SystemServiceImpl systemService;

    @GetMapping("/getToken")
    @ResponseBody
    public Map getToken(HttpServletRequest request) {
        Map<String,String> map = new HashMap<>();
        map.put("token", (String) request.getSession().getAttribute("token"));
        return map;
    }

    @RequestMapping("/loginOut")
    public String loginOut(HttpServletRequest request) {
        if (request.getSession().getAttribute("token")!=null){
            request.getSession().setAttribute("token", "");
        }
        return "admin/mould/page/login-3";
    }


    @GetMapping("/toAdminIndex")
    @CrossOrigin
    public String toAdminIndex(ModelMap map,String userAccount){
        HomeMessageQueue homeMessageQueue = (HomeMessageQueue) SpringContextUtils.getBean("homeMessageQueue");
        Queue<HomeMessage> homeMessageQueue1 = homeMessageQueue.getHomeMessageQueue();
        List<Post> posts = (List<Post>) postService.queryAllPost().getData().get("posts");
        List<CommunityInformation> communityInformations = (List<CommunityInformation>) communityInformationService.findCommunityInformationAll().getData().get("items");

        map.put("userName",userAccount);
        for (int i = 0; i < posts.size(); i++) {
            if (i == 3){
                break;
            }
            homeMessageService.add(posts.get(i));
        }

        for (int i = 0; i < communityInformations.size(); i++) {
            if (i == 3){
                break;
            }
            homeMessageService.add(communityInformations.get(i));
        }



        return "admin/mould/index";
    }

    /**
     * 管理员整个首页跳转
     * @return
     */
    @GetMapping("/communityIndex")
    @CrossOrigin
    public String toCommunityIndex() {
        return "admin/mould/index";
    }


    /**
     * 驳回界面跳转
     * @return
     */
    @GetMapping("/reportReasonIndex")
    @CrossOrigin
    public String reportReasonIndex(String reportNo, ModelMap map) {
        map.put("reportNo", reportNo);
        return "admin/mould/page/table/report/edit";
    }


    /**
     * 管理员欢迎页跳转
     * @return
     */
    @GetMapping("/toAdminWelcome")
    @CrossOrigin
    public String toAdminWelcome(ModelMap map,HttpServletRequest request){

        int userCount = userInformationService.count();
        int communityCount = communityInformationService.findCommunityInformationAll().getCount();
        int postCount = postService.queryAllPost().getCount();

        int onlineCount = 0;
        Set<String> temp = new HashSet<>();
        if (request.getSession(false)!=null && request.getSession().getAttribute("userCount")!=null){
//            onlineCount = (Integer) request.getSession().getAttribute("userCount");
            LoginLogList loginLogList = (LoginLogList) SpringContextUtils.getBean("loginLogList");
            List<LoginLog> loginLogList1 = loginLogList.getLoginLogList();
            for (LoginLog loginLog : loginLogList1) {
                temp.add(loginLog.getName());
            }
        }
        map.put("userCount", userCount);
        map.put("communityCount", communityCount);
        map.put("postCount", postCount);
        map.put("onlineCount",temp.size());

        return "admin/mould/page/welcome-1";
    }

    /**
     * 社区信息管理界面跳转
     * @return
     */
    @GetMapping("/toCommunityInfoIndex")
    @CrossOrigin
    public String toCommunityInfoIndex(){
        return "admin/mould/page/community";
    }

    /**
     * 菜单管理界面跳转
     * @return
     */
    @GetMapping("/toMenuIndex")
    @CrossOrigin
    public String toMenuIndex(){
        return "admin/mould/page/menu";
    }

    /**
     * 贴子管理界面跳转
     * @return
     */
    @GetMapping("/toPostIndex")
    @CrossOrigin
    public String toPostIndex(){
        return "admin/mould/page/postTable";
    }

    /**
     * 用户信息管理界面跳转
     * @return
     */
    @GetMapping("/toUserInfoIndex")
    @CrossOrigin
    public String toUserInfoIndex(){
        return "admin/mould/page/userInfoTable";
    }


    /**
     * 举报管理界面跳转
     * @return
     */
    @GetMapping("/toReportIndex")
    @CrossOrigin
    public String toReport(){
        return "admin/mould/page/table/report/report";
    }

    /**
     * 系统设置界面跳转
     * @return
     */
    @GetMapping("/toSystemSetIndex")
    @CrossOrigin
    public String toSystemSetIndex(ModelMap map){
        SystemSetting systemSetting = systemService.find();
        map.put("siteName",systemSetting.getSiteName());
        map.put("siteUrl",systemSetting.getSiteUrl());
        map.put("title",systemSetting.getTitle());
        map.put("copyright",systemSetting.getCopyright());
        return "admin/mould/page/systemSetting";
    }


    /**
     * 登陆&资源日志界面跳转
     * @return
     */
    @GetMapping("/toLoginLogIndex")
    @CrossOrigin
    public String toLoginLogIndex(){
        return "admin/mould/page/LoginLogTable";
    }


    /**
     *跳转社区添加
     * @return
     */
    @GetMapping("/toCommunityAdd")
    @CrossOrigin
    public String toCommunityAdd(){
        return "admin/mould/page/table/add";
    }
    /**
     *跳转登陆界面
     * @return
     */
    @GetMapping("/toLoginIn")
    public String toLogin(){

        return "admin/mould/page/login-3";
    }


    /**
     *跳转社区编辑
     * @return
     */
    @GetMapping("/toCommunityEdit")
    @CrossOrigin
    public String toCommunityEdit(String communityNo, ModelMap map){
        map.put("communityNo",communityNo);
        return "admin/mould/page/table/edit";
    }


    /**
     *跳转贴子编辑
     * @return
     */
    @GetMapping("/toPostEdit")
    @CrossOrigin
    public String toPostEdit(String postNo, ModelMap map){
        map.put("postNo",postNo);
        return "admin/mould/page/table/post/edit";
    }


}